package com.myblog.demo.service;

import com.myblog.demo.entity.UserInfo;
import com.myblog.demo.mapper.ArticleMapper;
import com.myblog.demo.mapper.CommentMapper;
import com.myblog.demo.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommentMapper commentMapper;

    public int reg(UserInfo userInfo) {
        return userMapper.reg(userInfo);
    }

    public UserInfo login(String username) {
        return userMapper.login(username);
    }

    public UserInfo getUserInfoById(Integer id) {
        return userMapper.getUserInfoById(id);
    }

    public int savePhotoPath(String path, Integer id) {
        return userMapper.savePhotoPath(path, id);
    }

    public int updateNickname(String nickname, Integer id) {
        return userMapper.updateNickname(nickname, id);
    }

    public int updateGitee(String gitee, Integer id) {
        return userMapper.updateGitee(gitee, id);
    }

    public HashMap<String, Integer> getTotalRCountAndCommentByUid(Integer uid) {
        HashMap<String, Integer> map = new HashMap<>();
        int rCounts = articleMapper.getTotalRCountByUId(uid);
        int comments = commentMapper.getTotalCommentsByUid(uid);
        map.put("rCounts", rCounts);
        map.put("comments", comments);
        return map;
    }

    // 当用户密码输入错误时，根据用户名增加 state 值
    public void addStateByUsername(String username) {
        userMapper.addStateByUsername(username);
    }

    // 当用户成功登录后，state 恢复默认值0
    public void initStateByUsername(String username) {
        userMapper.initStateByUsername(username);
    }

    // 获取指定用户的 state
    public int getStateByUsername(String username) {
        return userMapper.getStateByUsername(username);
    }

    // 更新用户的冻结时间
    public void updateUpdateTime(UserInfo userInfo) {
        userMapper.updateUpdateTime(userInfo);
    }
}
